﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class Account_kcgl_kcdd : System.Web.UI.Page
{
    DataClassesDataContext db = new DataClassesDataContext();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindData();
        }
    }
    private void BindData()
    {
        var resultZy = (from p in db.proc_GetExamPlace()
                        where p.wheClass == 1
                        select p).ToList();

        DataTable dt = new DataTable();
        dt.Columns.Add(new DataColumn("classNo"));
        dt.Columns.Add(new DataColumn("maxNum"));
        dt.Columns.Add(new DataColumn("place"));
        dt.Columns.Add(new DataColumn("canTestSub"));
        foreach (proc_GetExamPlaceResult item in resultZy)
        {
            var ResSubject = (from p in db.proc_GetSubClass(item.classNo)
                              select p).ToList();
            string TestSub = "";
            foreach (proc_GetSubClassResult itemS in ResSubject)
            {
                TestSub += itemS.subName + "、";
            }
            if (TestSub != "")
                TestSub = TestSub.Substring(0, TestSub.Length - 1);
            DataRow dr = dt.NewRow();
            dr["classNo"] = item.classNo;
            dr["maxNum"] = item.maxNum;
            dr["place"] = item.place;
            dr["canTestSub"] = TestSub;
            dt.Rows.Add(dr);
        }
        GridView2.DataSource = dt;
        GridView2.DataBind();
    }
    protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Label lb1 = (Label)e.Row.Cells[0].FindControl("Label1");
            lb1.Text = (GridView2.PageIndex * GridView2.PageSize + e.Row.RowIndex + 1).ToString();
        }
    }
    protected void GridView2_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        // 得到该控件
        GridView theGrid = sender as GridView;
        int newPageIndex = 0;
        if (e.NewPageIndex == -3)
        {
            //点击了Go按钮
            TextBox txtNewPageIndex = null;

            //GridView较DataGrid提供了更多的API，获取分页块可以使用BottomPagerRow 或者TopPagerRow，当然还增加了HeaderRow和FooterRow
            GridViewRow pagerRow = theGrid.BottomPagerRow;

            if (pagerRow != null)
            {
                //得到text控件
                txtNewPageIndex = pagerRow.FindControl("txtNewPageIndex") as TextBox;
            }
            if (txtNewPageIndex != null)
            {
                //得到索引
                newPageIndex = int.Parse(txtNewPageIndex.Text) - 1;
            }
        }
        else
        {
            //点击了其他的按钮
            newPageIndex = e.NewPageIndex;
        }
        //防止新索引溢出
        newPageIndex = newPageIndex < 0 ? 0 : newPageIndex;
        newPageIndex = newPageIndex >= theGrid.PageCount ? theGrid.PageCount - 1 : newPageIndex;

        //得到新的值
        BindData();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {

        bool flag = false;
        DataTable dt = new DataTable();
        dt.Columns.Add(new DataColumn("classNo"));
        dt.Columns.Add(new DataColumn("place"));
        dt.Columns.Add(new DataColumn("cantest"));
        for (int i = 0; i < GridView2.Rows.Count; i++)
        {
            System.Web.UI.WebControls.CheckBox checkBox =
                (System.Web.UI.WebControls.CheckBox)GridView2.Rows[i].FindControl("CheckBox1");
            if (checkBox.Checked == true)
            {
                Panel1.Visible = true;
                flag = true;
                DataRow dr = dt.NewRow();
                dr["classNo"] = GridView2.DataKeys[i].Value.ToString();
                dr["place"] = GridView2.Rows[i].Cells[2].Text;

                dt.Rows.Add(dr);

            }
        }
        if (flag == false)
        {
            ClientScript.RegisterClientScriptBlock(this.GetType(), "error", "<script>alert('请先勾选考场！')</script>");
            return;
        }

        GridView3.DataSource = dt;
        GridView3.DataBind();
    }
    protected void Button2_Click(object sender, EventArgs e)     //保存
    {
        for (int i = 0; i < GridView3.Rows.Count; i++)
        {
            bool bz = false;
            string classno = GridView3.Rows[i].Cells[0].Text;
            System.Web.UI.WebControls.DataList DL =
                (System.Web.UI.WebControls.DataList)GridView3.Rows[i].FindControl("DataList1");

            for (int j = 0; j < DL.Items.Count; j++)
            {

                CheckBox chbox = (CheckBox)DL.Items[j].FindControl("CheckBox2");
                //Label la = (Label)DL.Items[j].FindControl("subNameLabel");
                HiddenField hidden = (HiddenField)DL.Items[j].FindControl("HiddenField1");
                if (chbox.Checked)
                {
                    string subnum = hidden.Value;
                    db.proc_AddSubClass(classno, subnum);
                    ClientScript.RegisterClientScriptBlock(this.GetType(), "success", "<script>alert('添加成功(由于考场发生变化，考场安排已清空)！')</script>");
                    GridView3.DataSource = "";
                    GridView3.DataBind();
                    BindData();
                    Panel1.Visible = false;
                    bz = true;
                }

            }
            if (bz == false)
            {
                ClientScript.RegisterClientScriptBlock(this.GetType(), "error", "<script>alert('请先勾选课程！')</script>");
            }
        }
    }

    protected void Button3_Click(object sender, EventArgs e)    //取消
    {
        Panel1.Visible = false;
    }
}